package com.edol.painting.main;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.edol.painting.R;
import com.edol.painting.data.PaintingDatabase;
import com.edol.painting.utils.Constant;

import java.util.List;

/**
 * Created by Administrator on 2017/7/3.
 */

public class PaintController {
    private final static String TAG_LIST = "paint_list";
    private final static String TAG_PAGE = "paint_page";
    private final static String TAG_EDIT = "paint_edit";

    private static PaintController mInstance;

    private PaintController() {

    }

    public synchronized static PaintController getInstances() {
        if (mInstance == null) {
            mInstance = new PaintController();
        }

        return mInstance;
    }

    public void enterPaintListPage(Context context) {
        if (context instanceof FragmentActivity) {
            FragmentTransaction transaction = ((FragmentActivity) context).getSupportFragmentManager().beginTransaction();
            PaintListFragment fragment = new PaintListFragment();
            transaction
                    .add(R.id.content_layout, fragment)
                    .commit();

        }
    }

    public void enterPaintPreviewPage(Context context, List<PaintingDatabase.PaintingEntry> data, int index) {
        if (context instanceof FragmentActivity) {
            FragmentTransaction transaction = ((FragmentActivity) context).getSupportFragmentManager().beginTransaction();
            PaintPageFragment fragment = new PaintPageFragment();
            fragment.setData(data, index);
            transaction
                    .add(R.id.content_layout, fragment)
                    .addToBackStack(TAG_PAGE)
                    .commit();
        }
    }

    public void notifyPaintListChange(Context context, List<PaintingDatabase.PaintingEntry> data) {
        FragmentManager fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
        List<Fragment> fragments = fragmentManager.getFragments();

        if (fragments != null && !fragments.isEmpty()) {
            for (Fragment fragment : fragments) {
                if (fragment instanceof PaintListFragment) {
                    ((PaintListFragment) fragment).setData(data);
                    break;
                }
            }
        }
    }

    public void enterBlankPaint(Context context) {
        startPaintEdit(context, -1, 0, false);
    }

    public void enterBlankPaint(Context context, long id, int theme) {
        startPaintEdit(context, id, theme, true);
    }

    public void startPaintEdit(Context context, long id, int theme, boolean fromPager) {
        if (context instanceof FragmentActivity) {
            PaintEditFragment f = new PaintEditFragment();
            Bundle args = new Bundle();
            args.putLong(Constant.ARG_KEY_PAINTING_ID, id);
            args.putInt(Constant.ARG_KEY_THEME_ID, theme);
            args.putBoolean(Constant.ARG_KEY_FROM_PAGER, fromPager);
            f.setArguments(args);
            try {
                FragmentTransaction ft = ((FragmentActivity) context).getSupportFragmentManager().beginTransaction();
                ft.add(R.id.content_layout, f)
                        .addToBackStack(TAG_EDIT)
                        .commit();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void showActionBar(Activity activity) {
        if (activity instanceof PaintMainActivity) {
            ((PaintMainActivity)activity).showActionBar();
        }
    }

    public static void hideActionBar(Activity activity) {
        if (activity instanceof PaintMainActivity) {
            ((PaintMainActivity)activity).hideActionBar();
        }
    }

    public static void unlockDrawer(Activity activity) {
        if (activity instanceof PaintMainActivity) {
            ((PaintMainActivity)activity).unLockDrawer();
        }
    }

    public static void lockDrawer(Activity activity) {
        if (activity instanceof PaintMainActivity) {
            ((PaintMainActivity)activity).lockDrawer();
        }
    }

    public void destroy() {

    }

}
